﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace SimplexEngine
{
    public class BackBufferRenderContext : RenderContext
    {
        protected ResolveTexture2D resolve_texture = null;
        protected RenderTarget2D render_target = null;

        public BackBufferRenderContext(String context_name, RenderSystem creator) : base(context_name, creator)
        {
            render_target = creator.GraphicsDevice.GetRenderTarget(0) as RenderTarget2D;
            render_targets.Add(render_target);
            // get presentation parameters
            PresentationParameters presentation_parameters = creator.GraphicsDevice.PresentationParameters;
            // create resolve texture
            resolve_texture = new ResolveTexture2D(creator.GraphicsDevice, presentation_parameters.BackBufferWidth, presentation_parameters.BackBufferHeight, 1, presentation_parameters.BackBufferFormat);
            texture_names.Add("BackBuffer");

            screen_effect_system.PushMaterial(new MRTLightingMaterial());
        }

        protected List<RenderTarget2D> render_targets = new List<RenderTarget2D>();
        protected List<String> texture_names = new List<String>();

        protected internal override List<RenderTarget2D> GetRenderTargets()
        {
            return render_targets;
        }

        protected override void OnDraw(GameTime game_time, Int64 frame_number)
        {
            base.OnDraw(game_time, frame_number);
            render_system.GraphicsDevice.ResolveBackBuffer(resolve_texture);
        }

        protected internal override List<String> GetTextureNames()
        {
            return texture_names;
        }

        protected internal override Texture2D GetTexture(String name)
        {
            if (name == "BackBuffer")
                return resolve_texture;

            return null;
        }
    }
}
